home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / wrapdemo.lha / demo / README < prev    next >
Encoding:
Text File  |  1993-03-25  |  5.0 KB  |  161 lines

  1. This is a demo of Real-Time Mapping of a reflection of a graphic onto
  2. a sphere.  It is system-friendly, it multitasks, it uses an Intuition
  3. Screen, and GASP! -- it's written in C.
  4.  
  5. It runs a little slow on a stock 68000 machine (5-10 fps) but on any
  6. accelerated machine, speed is decent.  It goes quite fine on a 1200.
  7. Remember, I wrote this overnight just to show the theory... If I wanted
  8. it to be as fast as possible, I would have written the entire demo
  9. section in assembly.  Anyhow, it will run much faster on a machine with
  10. some sort of fast memory (not all CHIP or PSEUDO-FAST).
  11.  
  12. By the way, I don't know what you consider to be real-time, but I use
  13. no precalculated frames.  I do calculate a mapping table but why not???
  14. The thing needs to be calculated at least once and it never changes.
  15. I don't use a pre-calc'ed table per se as I do the computations in the
  16. program (so the average joe can see how it's done).
  17.  
  18. Anyhow, I can imagine getting this thing to go much faster with just a
  19. few small changes which I might get around to later.
  20.  
  21. A tribute to people who want to write system friendlier code...
  22.  
  23. ------------------------------------------------------------------------
  24.  
  25. Now some theory...  The most difficult part of Realtime Raytracing as it's
  26. often been called incorrectly (it's really realtime ampping), is to
  27. compute the mapping.  Anyone with a little high school geometry who
  28. knows the formula for a sphere and that similar triangles have the same
  29. ratio per side should be able to figure this out (it's very straight-
  30. forward).  A simple RADIUS-16 (32 wide) sphere mapping follows:
  31.  
  32.  
  33. VOID CalculateMap()
  34. {
  35.   short  y,y1,x,x1;
  36.   long   x2;
  37.   double r;
  38.  
  39.   for(y=0; y<31; y++)
  40.     {
  41.       y1=y-15;
  42.       for(x=0; x<32; x++)
  43.         {
  44.           x1=x-15;
  45.           x2=(256-(y1*y1)-(x1*x1));
  46.           if (x2>0)
  47.             {
  48.               r=32.0/sqrt((double)x2);
  49.               r*=((double)x1);
  50.               r+=15.5;
  51.               x2=(short)r;
  52.               Xarray[y][x]=x2;
  53.             }
  54.           else
  55.             {
  56.               Xarray[y][x]=9999;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62.    Ahh, that was easy.  Just copy and paste right out of my demo code.
  63.  
  64. Now, once you've got a mapping, making a sphere bounce around with that
  65. mapping is about this hard....
  66.  
  67. VOID DoDemo()
  68. {
  69.   register short xx,yy;
  70.   short x,y,xdir,ydir,xxx,yyy;
  71.   ULONG *place;
  72.  
  73.   x=160; y=45;
  74.   xdir=-1; ydir=1;
  75.  
  76.   while((CurrentFront!=0)||(! LEFTMOUSE))
  77.     {
  78.       x+=xdir; if((x<10)||(x>278)) xdir=-xdir;
  79.       y+=ydir; if((y<40)||(y>123)) ydir=-ydir;
  80.       place=&ReflectPlane[0];
  81.       BP_CLEAR.dpt=(WORD *)&BMraw[NextFront][(y-6)*40];
  82.       for(yy=0;yy!=31;yy++)
  83.         {
  84.           if((yy&7)==0) SLAM_BLITTER(&BP_CLEAR);
  85.           output=0;
  86.           for(xx=0;xx!=31;xx++)
  87.             {
  88.               xxx=Xarray[yy][xx]+x;
  89.               if((xxx>0)&&(xxx<319))
  90.                 {
  91.                   yyy=Xarray[xx][yy]+y;
  92.                   if ((yyy>0)&&(yyy<199)) ReadPoint(xxx,yyy);
  93.                 }
  94.               output+=output;
  95.             }
  96.             *(place++)=output;
  97.         }
  98.       DrawBall(x,y,NextFront);
  99.       SetView(NextFront);
  100.     }
  101. }
  102.  
  103.    Yep, that's the entire main loop of my demo.  Calculates the sphere's
  104. position.  For every point on the sphere, it calculates a reflection
  105. (using ReadPoint and a table look-up).  It draws the output onto the
  106. ball and then draws the ball onto the sphere.  SetView is simply double
  107. buffering using Intuition (ick@! slow!! #$%$!$#) routines to be system
  108. friendly.
  109.  
  110. OH, SLAM_BLITTER is just a little call to make blitter programming easy
  111. from C.  I use it in a lot of SilverFox SoftWare programs.  Anyhow, in
  112. this case, it just clears the old ball image.
  113.  
  114. Note that ReadPoint will note a point being found by incrementing output.
  115.  
  116.  
  117. ***** VOID __regargs ReadPoint(long int sX(d0), long int sY(d1));
  118. @ReadPoint::
  119.      move.l    d2,a1          ;Save d2 in a1
  120.      BASEREG   a4
  121.  
  122.      move.l    d1,d2          ; d1*=MOD (40)
  123.      lsl.l     #2,d2
  124.      add.l     d2,d1
  125.      lsl.l     #3,d1
  126.  
  127.      move.l    d0,d2
  128.      andi.w    #15,d2         ;d0=sX-=(d2=(sx&15))
  129.  
  130.      sub.l     d2,d0
  131.      lsr.l     #3,d0          ;d0=sX>>3
  132.      add.l     d1,d0          ;d0=(sX>>3)+(MOD*sY)
  133.      add.l     _RStartPlane(a4),d0 ;d0=StartPlane[d0]
  134.      move.l    #$8000,d1      ;Pixel=(0x8000>>d1)
  135.      lsr.l     d2,d1
  136.  
  137.      movea.l   d0,a0          ;Read(OffSet,Pixel)
  138.      and.w     (a0),d1
  139.  
  140.      beq.s     rp_nopixel
  141.      addq.l    #1,_output(a4)
  142. rp_nopixel
  143.  
  144.      BASEREG   OFF
  145.      move.l    a1,d2          ;Restore d2 from a1
  146.      rts
  147.  
  148.  
  149.    OK, so I lied.  I used a little ASM but the system call ReadPixel was
  150. way to slow, and besides... I didn't write my ASM for speed but more for
  151. clarity (really, I could have used offset table lookups and pseudo C
  152. equivalents are given for the code).  Besides, anyone can write their own
  153. write/read pixel routine or you shouldn't even be trying to program demos.
  154.  
  155. -------------------------------------------------------------------------
  156.  
  157. Written By Adisak Pochanayon  pochanay@cae.wisc.edu
  158.  
  159. Check out SilverFox SoftWare's games wherever you go...
  160.  
  161.